package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.dao.WmsProvinceRankingReportMapper;
import com.jy.wms.pojo.SalesChannelOrderStatusSummary;
import com.jy.wms.pojo.WmsOrderChannel;
import com.jy.wms.pojo.WmsProvinceRankingReport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.jy.wms.common.base.BaseController.getJSONObject;
@Service
public class SalesChannelOrderStatusSummaryService extends BaseService<WmsProvinceRankingReportMapper, WmsProvinceRankingReport> {

    /*
       各销售渠道订单状态汇总
    */
    public JSONObject saleschannelorderstatussummary(String ckid, String hzid, Map<String, Object> mapp) {
        JSONObject json = getJSONObject();
        //获取时间
        String date = (String) mapp.get("date");
        int page = (int) mapp.get("page");
        int rows = (int) mapp.get("rows");
        PageHelper.startPage(page, rows);
        Page<SalesChannelOrderStatusSummary> Thenumber = new Page<>();

        List<SalesChannelOrderStatusSummary> level = dao.Querylevel(ckid, hzid, date);

        for (SalesChannelOrderStatusSummary user : level) {
            SalesChannelOrderStatusSummary scoss = new SalesChannelOrderStatusSummary();
            scoss.setPurpose(user.getPurpose());
            if (!"".equals(user.getDdqdid())) {
                WmsOrderChannel ddqd = dao.ShippingOrderLevel(ckid, hzid, user.getDdqdid());
                if ("0".equals(ddqd.getFjd())) {
                    scoss.setClassA(ddqd.getMc());
                } else {
                    WmsOrderChannel ddqd2 = dao.ShippingOrderLevel(ckid, hzid, ddqd.getFjd());
                    if ("0".equals(ddqd2.getFjd())) {
                        scoss.setClassA(ddqd2.getMc());
                        scoss.setClassB(ddqd.getMc());
                    } else {
                        WmsOrderChannel ddqd3 = dao.ShippingOrderLevel(ckid, hzid, ddqd2.getFjd());
                        if ("0".equals(ddqd3.getFjd())) {
                            scoss.setClassA(ddqd3.getMc());
                            scoss.setClassB(ddqd2.getMc());
                            scoss.setClassC(ddqd.getMc());
                        }
                    }
                }

                //全部订单
                List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary fold : FullOrder) {
                    scoss.setOrdernumber(fold.getOrdernumber());
                    scoss.setTotalnumber(fold.getTotalnumber());
                    scoss.setTotalsales(fold.getTotalsales());
                }
                //已发货订单
                List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary alredy : Already) {
                    scoss.setTotalamount(alredy.getTotalamount());
                    scoss.setAlready(alredy.getAlready());
                    scoss.setSupportnumber(alredy.getSupportnumber());
                    scoss.setBoxes(alredy.getBoxes());
                }
                //未发货
                List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                    scoss.setUnconsignment(uspd.getUnconsignment());
                    scoss.setUnshipped(uspd.getUnshipped());
                    scoss.setUnconsignmentcount(uspd.getUnconsignmentcount());
                }
                //退单
                List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary rf : Refund) {
                    scoss.setRefund(rf.getRefund());
                    scoss.setOrderquantity(rf.getOrderquantity());
                    scoss.setRegression(rf.getRegression());
                }

            } else {

                scoss.setClassA(user.getClassA());
                scoss.setClassB(user.getClassB());
                scoss.setClassC(user.getClassC());

                //全部订单
                List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary fold : FullOrder) {
                    scoss.setOrdernumber(fold.getOrdernumber());
                    scoss.setTotalnumber(fold.getTotalnumber());
                    scoss.setTotalsales(fold.getTotalsales());
                }
                //已发货订单
                List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary alredy : Already) {
                    scoss.setTotalamount(alredy.getTotalamount());
                    scoss.setAlready(alredy.getAlready());
                    scoss.setSupportnumber(alredy.getSupportnumber());
                    scoss.setBoxes(alredy.getBoxes());
                }
                //未发货
                List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                    scoss.setUnconsignment(uspd.getUnconsignment());
                    scoss.setUnshipped(uspd.getUnshipped());
                    scoss.setUnconsignmentcount(uspd.getUnconsignmentcount());
                }
                //退单
                List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary rf : Refund) {
                    scoss.setRefund(rf.getRefund());
                    scoss.setOrderquantity(rf.getOrderquantity());
                    scoss.setRegression(rf.getRegression());
                }
            }
            Thenumber.add(scoss);
        }
        JSONArray array = JSONArray.fromObject(Thenumber);
        json = SetStatus.setSearchType(array.toString(), (int) Thenumber.getTotal());
        return json;
    }

    /*
        各销售渠道订单状态汇总报表导出
     */
    public List<SalesChannelOrderStatusSummary> saleschannelorderstatussummary(SalesChannelOrderStatusSummary SalesChannelOrderStatusSummary) {
        //获取用户跟id
        String ckid=SalesChannelOrderStatusSummary.getCkid();
        String hzid=SalesChannelOrderStatusSummary.getHzid();
        //时间
        String date=SalesChannelOrderStatusSummary.getDate();
        List<SalesChannelOrderStatusSummary> Thenumber = new Page<>();

        List<SalesChannelOrderStatusSummary> level=dao.QuerylevelExport(SalesChannelOrderStatusSummary);

        for (SalesChannelOrderStatusSummary user : level) {
            SalesChannelOrderStatusSummary scoss=new SalesChannelOrderStatusSummary();
            scoss.setPurpose(user.getPurpose());
            if(!"".equals(user.getDdqdid())){
                WmsOrderChannel ddqd=dao.ShippingOrderLevel(ckid,hzid,user.getDdqdid());
                if("0".equals(ddqd.getFjd())){
                    scoss.setClassA(ddqd.getMc());
                }else{
                    WmsOrderChannel ddqd2=dao.ShippingOrderLevel(ckid,hzid,ddqd.getFjd());
                    if("0".equals(ddqd2.getFjd())){
                        scoss.setClassA(ddqd2.getMc());
                        scoss.setClassB(ddqd.getMc());
                    }else{
                        WmsOrderChannel ddqd3=dao.ShippingOrderLevel(ckid,hzid,ddqd2.getFjd());
                        if("0".equals(ddqd3.getFjd())){
                            scoss.setClassA(ddqd3.getMc());
                            scoss.setClassB(ddqd2.getMc());
                            scoss.setClassC(ddqd.getMc());
                        }
                    }
                }

                //全部订单
                List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary fold : FullOrder) {
                    scoss.setOrdernumber(fold.getOrdernumber());
                    scoss.setTotalnumber(fold.getTotalnumber());
                    scoss.setTotalsales(fold.getTotalsales());
                }
                //已发货订单
                List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary alredy : Already) {
                    scoss.setTotalamount(alredy.getTotalamount());
                    scoss.setAlready(alredy.getAlready());
                    scoss.setSupportnumber(alredy.getSupportnumber());
                    scoss.setBoxes(alredy.getBoxes());
                }
                //未发货
                List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                    scoss.setUnconsignment(uspd.getUnconsignment());
                    scoss.setUnshipped(uspd.getUnshipped());
                    scoss.setUnconsignmentcount(uspd.getUnconsignmentcount());
                }
                //退单
                List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, date, scoss.getClassA(), scoss.getClassB());
                for (SalesChannelOrderStatusSummary rf : Refund) {
                    scoss.setRefund(rf.getRefund());
                    scoss.setOrderquantity(rf.getOrderquantity());
                    scoss.setRegression(rf.getRegression());
                }

            }else {

                scoss.setClassA(user.getClassA());
                scoss.setClassB(user.getClassB());
                scoss.setClassC(user.getClassC());

                //全部订单
                List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary fold : FullOrder) {
                    scoss.setOrdernumber(fold.getOrdernumber());
                    scoss.setTotalnumber(fold.getTotalnumber());
                    scoss.setTotalsales(fold.getTotalsales());
                }
                //已发货订单
                List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary alredy : Already) {
                    scoss.setTotalamount(alredy.getTotalamount());
                    scoss.setAlready(alredy.getAlready());
                    scoss.setSupportnumber(alredy.getSupportnumber());
                    scoss.setBoxes(alredy.getBoxes());
                }
                //未发货
                List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                    scoss.setUnconsignment(uspd.getUnconsignment());
                    scoss.setUnshipped(uspd.getUnshipped());
                    scoss.setUnconsignmentcount(uspd.getUnconsignmentcount());
                }
                //退单
                List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, date, user.getClassA(), user.getClassB());
                for (SalesChannelOrderStatusSummary rf : Refund) {
                    scoss.setRefund(rf.getRefund());
                    scoss.setOrderquantity(rf.getOrderquantity());
                    scoss.setRegression(rf.getRegression());
                }
            }
            Thenumber.add(scoss);
        }
        return Thenumber;
    }
}
